package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import util.GetTime;


public class DataCommentDAO {
	
	//자료실 꼬리말 달기
	public boolean insert(DataComment dc){
		Connection conn = ConnectionManager.getConnection();
		try{
			PreparedStatement ps = conn.prepareStatement(
					//@@ "insert into datacomment(dbno,mid,dccomment,dcdate,iconno) values(?,?,?,now(),?)");
			"insert into datacomment(dcno,dbno,mid,dccomment,dcdate,iconno) values(DCSEQ.NEXTVAL,?,?,?,?,?)");
			ps.setInt(1, dc.getDbno());
			ps.setString(2, dc.getMid());
			ps.setString(3, dc.getDccomment());	
			System.out.println(dc.getIconno());
			ps.setTimestamp(4, GetTime.nowTime);
			ps.setInt(5, dc.getIconno());			
			
			ps.executeUpdate();
			
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			if(conn!=null){
				try {
					conn.close();
				} catch (Exception e) {	}
			}
		}		
	}
	
	//자료실 꼬리말 삭제
	public boolean delete(DataComment dc){
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement st = conn.createStatement();
			st.executeUpdate("delete from datacomment where dcno="+dc.getDcno());
			
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			if(conn!=null){
				try {
					conn.close();
				} catch (Exception e) {	}
			}
		}		
	}	
	
	//특정 글에 대한  꼬리말 삭제
	public boolean deleteForData(int dbno){
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement st = conn.createStatement();
			st.executeUpdate("delete from datacomment where dbno="+dbno);
			
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			if(conn!=null){
				try {
					conn.close();
				} catch (Exception e) {	}
			}
		}		
	}
	
	//자료실 전체  꼬리말 수 
	public int getTotalRows(){
		int totalRows=0;
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select count(*) from datacomment");
			rs.next();
			totalRows = rs.getInt(1);			
			rs.close();
			st.close();
			
		}catch(Exception e){
			e.printStackTrace();			
		}finally{
			if(conn!=null){
				try {
					conn.close();
				} catch (Exception e) {	}
			}
		}
		return totalRows;
	}
	
	//특정 자료실 글  꼬리말 수 
	public int getCommentRows(int dbno){
		int commentRows=0;
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select count(*) from datacomment where dbno="+dbno);
			rs.next();
			commentRows = rs.getInt(1);			
			rs.close();
			st.close();
			
		}catch(Exception e){
			e.printStackTrace();			
		}finally{
			if(conn!=null){
				try {
					conn.close();
				} catch (Exception e) {	}
			}
		}
		return commentRows;
	}
	
	//자료실 꼬리말 읽기 
	public DataComment getDataComment(int dcno){
		Connection conn = ConnectionManager.getConnection();
		DataComment dcomment = null;
		try{
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select * from datacomment where dcno="+dcno);
			if(rs.next()){
				dcomment = new DataComment();				
				dcomment.setDcno(rs.getInt(1));
				dcomment.setDbno(rs.getInt(2));
				dcomment.setMid(rs.getString(3));
				dcomment.setDccomment(rs.getString(4));
				dcomment.setDcdate(rs.getString(5));
				dcomment.setIconno(rs.getInt(6));
			}
			rs.close();
			st.close();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null){
				try {
					conn.close();
				} catch (Exception e) {	}
			}
		}
		return dcomment;
	}
	
	//입력한 꼬리말 번호 불러오기
	public int getMaxDcno(DataComment dc){
		int dcno=0;
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select max(dcno) from datacomment where mid='"+dc.getMid()+"'");
			if(rs.next()){
				dcno = rs.getInt(1);
			}
			rs.close();
			st.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null){
				try {
					conn.close();
				} catch (Exception e) {	}
			}
		}
		return dcno;
	}
	
	//특정 게시물 꼬리말 가져오기
	public Vector<DataComment> getList(int dbno){
		Vector list = new Vector();
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select * from datacomment where dbno="+dbno);
			while(rs.next()){
				DataComment dcomment = new DataComment();
				dcomment = new DataComment();				
				dcomment.setDcno(rs.getInt(1));
				dcomment.setDbno(rs.getInt(2));
				dcomment.setMid(rs.getString(3));
				dcomment.setDccomment(rs.getString(4));
				dcomment.setDcdate(rs.getString(5));
				dcomment.setIconno(rs.getInt(6));
				
				list.add(dcomment);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null){
				try{
					conn.close();
				}catch(Exception e ){}
			}
		}
		return list;
	}
	//자료실 꼬리말 목록
	public Vector getList(){
		Vector list = new Vector();
		Connection conn = ConnectionManager.getConnection();
		try{
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery("select * from datacomment");
			while(rs.next()){
				DataComment dcomment = new DataComment();
				dcomment = new DataComment();				
				dcomment.setDcno(rs.getInt(1));
				dcomment.setDbno(rs.getInt(2));
				dcomment.setMid(rs.getString(3));
				dcomment.setDccomment(rs.getString(4));
				dcomment.setDcdate(rs.getString(5));
				dcomment.setIconno(rs.getInt(6));
				
				list.add(dcomment);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(conn!=null){
				try{
					conn.close();
				}catch(Exception e ){}
			}
		}
		return list;
	}

}
